<?php

namespace App\Service\GuildManage;

use App\Model\GuildManage\GuildWithdrawal;
use App\Service\BaseService;
use Hyperf\Di\Annotation\Inject;
class WithdrawalListService extends BaseService
{
    use \Hyperf\Di\Aop\ProxyTrait;
    use \Hyperf\Di\Aop\PropertyHandlerTrait;
    function __construct(\Hyperf\AsyncQueue\Driver\DriverFactory $driverFactory)
    {
        if (method_exists(parent::class, '__construct')) {
            parent::__construct(...func_get_args());
        }
        $this->__handlePropertyHandler(__CLASS__);
    }
    /**
     * @Inject
     * @var GuildWithdrawal
     */
    protected $guildWithdrawalModel;
    /**
     * 返回
     * @var
     */
    private $return;
    public function handle()
    {
        $this->actionUserId = $this->getUserId();
        $this->return = ['total' => 0, 'total_page' => 0, 'page' => 0, 'list' => []];
        $page = $this->request->input('page');
        $limit = 20;
        $this->manageData($this->getUserData($page, $limit));
        return $this->return;
    }
    private function getUserData($page, $limit)
    {
        $where[] = ['user_id', '=', $this->actionUserId];
        $this->return['total'] = $this->guildWithdrawalModel->where($where)->count();
        $this->return['total_page'] = (int) ceil($this->return['total'] / $limit);
        $this->return['page'] = (int) $page;
        return $this->guildWithdrawalModel->where($where)->offset(((int) $page - 1) * $limit)->limit($limit)->select('withdraw_real_name', 'withdraw_account', 'withdraw_rmb', 'created_at', 'withdraw_type', 'yzh_detail', 'status')->orderBy('id', 'desc')->get();
    }
    /**
     * 处理数据
     * @param $data
     * @return void
     */
    private function manageData($data)
    {
        foreach ($data as $one) {
            $oneStatus = '';
            switch ($one->status) {
                case GuildWithdrawal::STATUS_ING:
                    $oneStatus = '审核中';
                    break;
                case GuildWithdrawal::STATUS_OK:
                    $oneStatus = '已提现';
                    break;
                case GuildWithdrawal::STATUS_FAIL:
                    $oneStatus = '审核不通过';
                    break;
                case GuildWithdrawal::STATUS_YZH_ERR:
                    $oneStatus = '提现失败';
                    break;
            }
            $this->return['list'][] = ['withdraw_real_name' => $one->withdraw_real_name, 'withdraw_account' => $one->withdraw_account, 'status' => $oneStatus, 'sub_status' => $one->status, 'withdraw_rmb_string' => '金额：' . $one->withdraw_rmb, 'withdraw_type' => '提现方式：' . ($one->withdraw_type == 1 ? '银行卡' : '支付宝'), 'created_at' => (string) $one->created_at, 'yzh_detail' => $one->status == GuildWithdrawal::STATUS_YZH_ERR ? $this->buildMsg($one->yzh_detail) : ''];
        }
    }
    /**
     * 获取提现失败的消息
     * @param $detail
     * @return mixed|string
     */
    private function buildMsg($detail)
    {
        $msg = '';
        $detail = empty($detail) ? [] : json_decode($detail, true);
        if (!empty($detail['detail'])) {
            $msg = $detail['detail'];
        }
        return $msg;
    }
}